from enum import Enum
import datetime
import util.Encoder as Encoder
import util.tool as tool
from util.test1 import getConfig
import pprint
from dateutil.relativedelta import relativedelta

def setMeterNoResp(meterAddr):
        pass
def recoverMeterResp():
        pass

def getTerAddr(addr):
    add = ""
    for i in range(0,len(addr),2):
          add = addr[i:i+2] + add
    return add


class Apdu_Service:
        APDU_LINK_REQUEST = 0x01      #预连接请求      	    	[1]	 LINK-Request
        PDU_LINK_RESPONSE = 0x81     #预连接响应       	    [129]LINK-Response

        APDU_SECURITY_REQUEST = 0x10      #安全认证请求        	[4]	SecurityRequest
        APDU_SECURITY_RESPONSE = 0x90     #安全认证响应     	    [144]	SecurityResponse

        CLIENT_APDU_CONNECT = 0x02      #建立应用连接请求    	[2]	CONNECT-Request
        CLIENT_APDU_RELEASE = 0x03      #断开应用连接请求    	[3]	RELEASE-Request
        CLIENT_APDU_GET = 0x05      #读取请求            	[5]	GET-Request
        CLIENT_APDU_SET = 0x06      #设置请求            	[6]	SET-Request
        CLIENT_APDU_ACTION = 0x07   #操作请求            	[7]	ACTION-Request
        CLIENT_APDU_REPORT = 0x08   #上报应答            	[8]	REPORT-Response
        CLIENT_APDU_PROXY = 0x09    #代理请求            	[9]	PROXY-Request


        SERVER_APDU_CONNECT = 0x82      #建立应用连接请求    	[130]	CONNECT-Response
        SERVER_APDU_RELEASE_RESP = 0x83      #断开应用连接响应 	    [131]	RELEASE-Response
        SERVER_APDU_RELEASE_NOTI = 0x84      #断开应用连接通知  	[132]	RELEASE-Notification
        SERVER_APDU_GET = 0x85      #读取响应         	    [133]	GET-Response
        SERVER_APDU_SET = 0x86      #设置响应       	    [134]	SET-Response
        SERVER_APDU_ACTION = 0x87      #操作响应        	    [135]	ACTION-Response
        SERVER_APDU_REPORT = 0x88      #上报通知        	    [136]	REPORT-Notification
        SERVER_APDU_PROXY = 0x89     #代理响应          	    [137]	PROXY-Response
        SERVER_APDU_ERROR = 0xEE     #错误响应          	    [238]	ERROR-Response


        GET_REQUEST_NORMAL = 0x01      #1)	读取一个对象属性；
        GET_REQUEST_NORMAL_LIST = 0x02      #2)	读取多个对象属性；
        GET_REQUEST_RECORD = 0x03      #3)	读取一个记录型对象属性；
        GET_REQUEST_RECORD_LIST = 0x04      #4)	读取多个记录型对象属性；
        GET_REQUEST_NEXT = 0x05      #5)	读取分帧响应的下一个数据块，仅在被请求的数据响应不能在一个GET.response APDU中传输时才使用

        GET_RESPONSE_NORMAL = 0x01     #1)	读取一个对象属性；
        GET_RESPONSE_NORMAL_LIST = 0x02      #2)	读取多个对象属性；
        GET_RESPONSE_RECORD = 0x03      #3)	读取一个记录型对象属性；
        GET_RESPONSE_RECORD_LIST = 0x04     #4)	读取多个记录型对象属性；
        GET_RESPONSE_NEXT = 0x05      #5)	分帧响应一个数据块

        SET_REQUEST_NORMAL = 0x01     #设置一个对象属性请求
        SET_REQUEST_NORMAL_LIST = 0x02    #设置多个对象属性请求
        SET_THEN_GET_REQUEST_NORMAL_LIST = 0x03     #设置后读取多个对象属性请求

        SET_RESPONSE_NORMAL = 0x01

        

        ACTION_REQUEST = 0x01     #操作一个对象方法请求
        ACTION_REQUEST_LIST = 0x02     #操作多个对象方法请求
        ACTION_THEN_GET_REQUEST_NORMAL_LIST = 0x03     #操作多个对象方法后读取多个对象属性请求
        
        ACTION_RESPONSE_NORMAL = 0x01

        REPORT_RESPONSE = 0x08
        ReportResponseList = 0x01
        ReportResponseRecordList = 0x02

        REPORT_NOTIFY = 0x88
        ReportNotificationList = 0x01
        ReportNotificationRecordList = 0x02
        


class DAR:
        #数据错在结果DAR
        SUCCESS = 0       #成功
        HARDWARE_FAULT = 1       #硬件失效
        TEMPORARY_FAILURE = 2       #暂时失效
        READ_WRITE_DENIED = 3       #拒绝读写
        OBJECT_UNDEFINED = 4       #对象未定义
        OBJECT_CLASS_INCONSISTENT = 5       #对象接口类不符合
        OBJECT_UNAVAILABLE = 6       #对象不存在
        TYPE_UNMATCHED = 7       #类型不匹配
        SCOPE_OF_ACCESS_VIOLATED = 8      #越界
        DATA_BLOCK_UNAVAILABLE = 9      #数据块不可用
        LONG_GET_ABORTED = 10      #分帧传输已取消
        NO_LONG_GET_IN_PROGRESS = 11      #不处于分帧传输状态
        LONG_WRITE_ABORTED = 12      #块写取消
        NO_LONG_WRITE_IN_PROGRESS = 13      #不存在块写状态
        DATA_BLOCK_NUMBER_INVALID = 14      #数据块序号无效
        UNAUTHORIZED_ACCESS = 15      #密码错误、未授权
        COMM_RATE_NO_CHANGE = 16      #通信速率不能更改
        YEAR_TIME_ZONE_COUT_OVER = 17      #年时区数超
        DAY_TIME_INTERVAL_COUNT_VOER = 18      #日时段数超
        RATE_EXCEEDED = 19      #费率数超
        SECURITY_AUTH_UNMATCHED = 20      #安全认证不匹配
        DUPLICATE_RECHARGE = 21      #重复充值
        ESAM_VERIFY_FAIL = 22      #ESAM验证失败
        SECURITY_AUTH_FAIL = 23      #安全认证失败
        CUST_NO_UNMATCHED = 24      #客户编号不匹配
        RECHARGE_COUNT_ERROR = 25      #充值次数错误
        BUY_FEE_OVER = 26      #购电超囤积
        ADDRESS_EXCEPT = 27      #地址异常
        SYMMETRIC_DECRYPT_ERROR = 28      #对称解密错误
        ASYMMETRIC_DECRYPT_ERROR = 29     #非对称解密错误
        SIGNATURE_ERROR = 30      #签名错误
        METER_HANGING = 31      #电能表挂起
        TIME_TAG_INVALID = 32      #时间标签无效
        TIME_OUT = 33      #请求超时
        ESAM_P1P2_ERROR = 34      #ESAM的P1P2不正确
        ESAM_LC_ERROR = 35      #ESAM的LC错误
        OTHER_REASON = 255     #其它

def ServiceJudgment(APDU):
        if APDU == "":
              return "NULL"
        APDU = APDU.replace(" ","")
        server = APDU[0:2]
        server = int("0x"+server,16)
        match server:
                case Apdu_Service.SERVER_APDU_GET:
                        match APDU[2:4]:
                                case "01":
                                        return GetResponseNormal(APDU)
                                case "02":
                                        return 
                                case "03":
                                        return GetResponseRecord(APDU)
                                case "04":
                                        return GetResponseRecordList(APDU)
                                case "05":
                                        return GetRequestNext(APDU)
                case Apdu_Service.SERVER_APDU_SET:
                        match APDU[2:4]:
                                case "01":
                                        return SetResponseNormal(APDU)
                                case "02":
                                        return SetRequestNormalList(APDU)
                                case "03":
                                        return SetThenGetResponseNormalList(APDU)
                        # return SetResponseNormal(APDU)
                case Apdu_Service.SERVER_APDU_REPORT:
                        match APDU[2:4]:
                                case "01":
                                        return ReportNotificationList(APDU)
                                case "02":
                                        return ReportNotificationRecordList(APDU)
                                
                case Apdu_Service.SERVER_APDU_ACTION:
                        return ActionResponseNormal(APDU)
                case Apdu_Service.SERVER_APDU_PROXY:
                        return ProxyGetResponseList(APDU)
                case Apdu_Service.SERVER_APDU_ERROR:
                        return 238
                case Apdu_Service.PDU_LINK_RESPONSE:
                    return LINK_Response(APDU)
                case Apdu_Service.SERVER_APDU_CONNECT:
                    return CONNECT_Response(APDU)
                case Apdu_Service.SERVER_APDU_RELEASE_RESP:
                    return RELEASE_Response(APDU)
                case Apdu_Service.SERVER_APDU_RELEASE_NOTI:
                    return RELEASE_Notification(APDU)
def GetResponseNormalList(apdu):
      dic = {}
      if apdu == "":
        return dic
      server = apdu[:2]
      piid = apdu[2:4]
      num = apdu[4:6]
      
def RELEASE_Notification(apdu):
      dic = {}
      if apdu == "":
        return dic
      server = apdu[:2]
      piid = apdu[2:4]
      dic["establish_time"] = apdu[4:18]
      dic["server_time"] = apdu[18:32]
      return dic
def RELEASE_Response(apdu):
      dic = {}
      if apdu == "":
        return dic
      server = apdu[:2]
      piid = apdu[2:4]
      dic["result"] = apdu[4:6]
      return dic
def LINK_Response(apdu):
      if apdu == "":
        return result
      server = apdu[:2]
      piid = apdu[2:4]
      result = int(apdu[4:6],16)&1

      return result
def CONNECT_Response(apdu):
      dic = {}
      if apdu == "":
        return dic
      dic["server"] = apdu[:2]
      dic["piid"] = apdu[2:4]
      dic["FactoryVersion"] = apdu[4:68]
      dic["proVersion"] = apdu[68:72]
      dic["ProtocolConformance"] = apdu[72:88]
      dic["FunctionConformance"] = apdu[88:120]
      dic["serverSendFraMaxSize"] = apdu[120:124]
      dic["serverReciveFraMaxSize"] = apdu[124:128]
      dic["win"] = apdu[128:130]
      dic["MaxDeal"] = apdu[130:134]
      dic["layTime"] = apdu[134:142]
      dic["result"] = apdu[142:144]
      return dic

def ProxyGetResponseList(apdu):
        dic = {}
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        piid = apdu[4:6]
        TSA_len = apdu[6:8]
        addr = apdu[12:24]
        lens, oad, getResult, dataStr= Encoder.De_A_ResultNormal(apdu[26:])
        dic["dar"] = getResult[:2]
        dic["data"] = getResult[3:]
        return dic
def GetServerJudgment(oad,server,data = ""):
        oad =oad.replace(" ","")
        match server:
                case Apdu_Service.CLIENT_APDU_SET:
                        return SetRequestNormal(oad, data)
                case Apdu_Service.CLIENT_APDU_ACTION:
                        return ActionRequest(oad, data)
        
              
def GetRequestNormalList(data):
        apdu = ""
        apdu += tool.Dec2HexStr(Apdu_Service.CLIENT_APDU_GET)
        apdu += "02"
        apdu += "01"
        apdu += tool.int2str16(len(data))
        for i in data:
              apdu +=i.replace(" ","")
        apdu += "00"
        return apdu
      

def GetRequestNormal(OAD):
        if type(OAD)==list:
              oad = ""
              for i in OAD:
                      oad+=i
        else:
              oad = OAD
        apdu = ""
        apdu += tool.Dec2HexStr(Apdu_Service.CLIENT_APDU_GET)
        apdu += tool.Dec2HexStr(Apdu_Service.GET_REQUEST_NORMAL)
        apdu += "01"
        apdu += oad
        apdu += "00"
        return apdu

def GetResponseNormal(apdu:str):
        oad = ""
        result = ""
        data = ""
        apdu = apdu.replace(" ","")
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        piid = apdu[4:6]
        if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_GET) == srv_type and tool.Dec2HexStr(Apdu_Service.GET_RESPONSE_NORMAL) == itrf_type):
                oad = apdu[6: 14]
                result = apdu[14: 16]
                if (result == "01"):
                        if apdu[16:18] == "00":
                              data = "00"
                        else:
                              data = apdu[16:]
                        return oad,result,data
                else:
                       data = apdu[16:18]
                       return oad,result,data 

        else:
                return oad,result,data

def GetResponseRecord(apdu:str):
        A_ResultRecord_Str = ""
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_GET) == srv_type and tool.Dec2HexStr(Apdu_Service.GET_RESPONSE_RECORD) == itrf_type):
                piid = apdu[4:6]
                lens, oad, rcsd, dar, A_RecordRowStrList, dataStr = Encoder.De_A_ResultRecord(apdu[6:])
                return oad, rcsd, dar, A_RecordRowStrList, dataStr
        return "","","",[],""
def GetResponseRecord_dic(apdu:str):
      srv_type = apdu[0:2]
      itrf_type = apdu[2:4]
      if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_GET) == srv_type and tool.Dec2HexStr(Apdu_Service.GET_RESPONSE_RECORD) == itrf_type):
              piid = apdu[4:6]
              lens, oad, rcsd, dar, A_RecordRowStrList, dataStr = Encoder.Dec_A_ResultRecord(apdu[6:])
              return oad, rcsd, dar, A_RecordRowStrList, dataStr
      return "","","",[],""
def GetResponseRecordList(apdu:str):
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        pos = 8
        dataStrs= []
        dars = []
        oads = []
        data ={}
        if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_GET) == srv_type):
                piid = apdu[4:6]
                for i in range(int(apdu[6:8], 16)):
                      
                        lens,oad, rcsd, dar, A_RecordRowStrList, dataStr = Encoder.De_A_ResultRecord(apdu[pos:])
                        pos += lens*2
                        dataStrs.append(dataStr)
                        dars.append(dar)
                        oads.append(oad)
                        data[oad] = dataStr
                return oads, rcsd, dars, A_RecordRowStrList, dataStrs,data
        return "","","",[],""
def GetRequestNext(apdu):
        dic = {}
        dic["dar"] =""
        dic["data"] = ""
        dic["srv_type"] = apdu[0:2]
        dic["itrf_type"] = apdu[2:4]
        dic["piid"] = apdu[4:6]
        dic["end_label"] = apdu[6:8]
        dic["split_frame_index"]= apdu[8:12]
        dic["choice"] = apdu[12:14]
        if dic["choice"] == "00":
              dic["dar"] = apdu[14:16]
              return dic
        else:
              dic["data"] = apdu[14:]
              return dic
def SetRequestNormal(oad, data):
        apdu = ""
        apdu += f"0x{Apdu_Service.CLIENT_APDU_SET:02X}".replace("0x", "")
        apdu += f"0x{Apdu_Service.SET_REQUEST_NORMAL:02X}".replace("0x", "")
        apdu += "01"
        apdu += oad
        apdu += data
        apdu += "00"
        return apdu

def SetResponseNormal(apdu):
        apdu = apdu.replace(" ","")
        oad = ""
        dar = ""
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_SET) == srv_type and tool.Dec2HexStr(Apdu_Service.SET_RESPONSE_NORMAL) == itrf_type):
                # print("srv_type:",srv_type)
                oad = apdu[6: 14]
                dar = apdu[14: 16]
                return oad,dar

        else:
                return oad,dar

def SetRequestNormalList(apdu):
        apdu = apdu.replace(" ","")
        pos = 8
        dic = {}
        for i in range(int(apdu[6:8])):
              dic[apdu[pos:pos+8]] = apdu[pos+8:pos+10]
              pos +=10
        return dic
def SetThenGetResponseNormalList(apdu):
        apdu = apdu.replace(" ","")
        pos = 8
        dic_set = {}
        dic_read = {}
        for i in range(int(apdu[6:8],16)):
            oad_set = apdu[pos:pos+8]
            dar = apdu[pos+8:pos+10]
            dic_set[oad_set] = dar
            oad_read = apdu[pos+10:pos+18]
            read_type = apdu[pos+18:pos+20]
            if read_type == "00":
                datapos = 2
                data = apdu[pos+20:pos+22]
            else:
                getreuTyp = apdu[pos+20:pos+22]
                datapos = Encoder.JugementDatatypePos(apdu[pos+20:])

                data = apdu[pos+20:pos+20+datapos]
            dic_read[oad_read] = data
            pos = pos+ 20 + datapos
        return dic_set,dic_read
def ActionRequest(omd, data):
        apdu = ""
        apdu += f"0x{Apdu_Service.CLIENT_APDU_ACTION:02X}".replace("0x", "")
        apdu += f"0x{ Apdu_Service.ACTION_REQUEST:02X}".replace("0x", "")
        apdu += "01"
        apdu += omd
        apdu += data
        apdu += "00"
        return apdu

def ActionResponseNormal(apdu):
        apdu = apdu.replace(" ","")
        omd = ""
        dar = ""
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        if (tool.Dec2HexStr(Apdu_Service.SERVER_APDU_ACTION) == srv_type and tool.Dec2HexStr(Apdu_Service.ACTION_RESPONSE_NORMAL) == itrf_type):
                # print("srv_type:",srv_type)
                omd = apdu[6: 14]
                dar = apdu[14: 16]
                return omd,dar

        else:
                return omd,dar
def GetCompletionFra(OAD):
        OAD = OAD.replace(" ","")
        apdu = GetRequestNormal(OAD)
        return apdu
#88 01
def ReportNotificationList(apdu):
        oads = []
        A_ResultNormalStr_List =[]
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        if (tool.Dec2HexStr(Apdu_Service.REPORT_NOTIFY) == srv_type and tool.Dec2HexStr(Apdu_Service.ReportNotificationList) == itrf_type):
                piid = str(tool.HexStr2DecInt(apdu[4:6]))
                cnt = tool.HexStr2DecInt(apdu[6:8])
                pos = 8
                for i in range(cnt):
                        lens, oad, getResult, dataStr = Encoder.De_A_ResultNormal(apdu[8:])
                        pos = pos + int(lens) * 2
                        oads.append(oad)
                        A_ResultNormalStr_List.append(dataStr)
        return oads,A_ResultNormalStr_List,piid
#piidString = "01"

def ReportResponseList(piidStr:str, oads:list):
        apdu = tool.Dec2HexStr(Apdu_Service.REPORT_RESPONSE)
        apdu += tool.Dec2HexStr(Apdu_Service.ReportResponseList)
        apdu += piidStr
        cnt = len(oads)
        apdu += tool.Dec2HexStr(cnt)
        for i in range(cnt):
                apdu += oads[i]
        apdu += "00"
        return apdu

#88 02
def ReportNotificationRecordList(apdu):
        oads = []
        A_ResultRecord_Str_List =[]
        srv_type = apdu[0:2]
        itrf_type = apdu[2:4]
        if (tool.Dec2HexStr(Apdu_Service.REPORT_NOTIFY) == srv_type and tool.Dec2HexStr(Apdu_Service.ReportNotificationRecordList) == itrf_type):
                piid = apdu[4:6]
                cnt = tool.HexStr2DecInt(apdu[6:8])
                pos = 8
                for i in range(cnt):
                        #lens, oad, rcsd, dar, A_RecordRowStrList, dataStr[0:pos]
                        lens, oad, rcsd, dar, A_RecordRowStrList,dataStr = Encoder.De_A_ResultRecord(apdu[8:])
                        pos = pos + int(lens) * 2
                        oads.append(oad)
                        A_ResultRecord_Str_List.append(dataStr)
        return oads,A_ResultRecord_Str_List,piid

#piidStr = "00"
#回复确认
def ReportResponseRecordList(piidStr:str, oads:list):
        apdu = tool.Dec2HexStr(Apdu_Service.REPORT_RESPONSE)
        apdu += tool.Dec2HexStr(Apdu_Service.ReportResponseRecordList)
        apdu += piidStr
        cnt = len(oads)
        apdu += tool.Dec2HexStr(cnt)
        for i in range(cnt):
                apdu += oads[i]
        apdu += "00"
        return apdu
def ReportComfir(server, piidStr:str, oads:list):
        match server:
                case "01":
                        return ReportResponseList(piidStr, oads)
                case "02":
                        return ReportResponseRecordList(piidStr, oads)
if __name__ == "__main__":
        #len, oad, getResult, dataStr = Encoder.De_A_ResultNormal("40 00 02 00 01 1c 07 e8 01 1e 00 00 00".replace(" ", ""))
        #print("GetResult len:", len)
        #print("GetResult oad:", oad)
        #print("GetResult getResult:", getResult)
        #print("GetResult dataStr:", dataStr)

        #a = ReportNotificationList("88 01 11 01 40 00 02 00 01 1c 07 e8 01 1e 00 00 00 00 01 07 e8 01 1e 00 00 00 01 00 05 ".replace(" ", ""))
        #print("a:",a)

        #len, oad, rcsd, dar, A_RecordRowStrList, dataStr = Encoder.De_A_ResultRecord("60 12 03 00 01 01 50 04 02 00 02 00 10 02 00 00 20 02 00 01 03 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 59 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 60 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 61".replace(" ", ""))
        #print("RecordRow len:", len)
        #print("RecordRow oad:", oad)
        #print("RecordRow rcsd:", rcsd)
        #print("RecordRow dar:", dar)
        #print("RecordRow A_RecordRowStrList:", A_RecordRowStrList)
        #print("RecordRow dataStr:", dataStr)
        #oads,A_ResultRecord_Str_List = ReportNotificationRecordList("88 02 1f 01 60 12 03 00 01 01 50 04 02 00 02 00 10 02 00 00 20 02 00 01 03 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 02 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 01 05 06 00 00 07 d0 06 00 00 01 f4 06 00 00 01 90 06 00 00 01 f4 06 00 00 02 58 00 01 07 e8 01 1e 00 04 02 01 00 05".replace(" ", ""))
        #print("oads:", oads)
        #print("A_ResultRecord_Str_List:", A_ResultRecord_Str_List)

        #oad, rcsd, dar, A_RecordRowStrList, dataStr = GetResponseRecord("85 03 10 31 0a 02 00 03 00 20 22 02 00 00 20 1e 02 00 00 20 20 02 00 01 01 06 00 00 00 00 1c 07 e8 01 1b 00 00 22 00 00 00".replace(" ",""))
        #print("oad:",oad)
        #print("rcsd:",rcsd)
        #print("dar:",dar)
        #print("A_RecordRowStrList:",A_RecordRowStrList)
        #print("dataStr:",dataStr)

        #_,csds,_ = Encoder.De_RCSD(rcsd.upper())
        #Encoder.De_A_RecordRow("06000000001c07e8011b00002200".upper(), len(csds))

        #apdu = ReportResponseRecordList("01",["40000200","40000201"])
        #print("apdu:", apdu)
        #Encoder.En_Long(100)
        Encoder.En_Long(-370)
        print("short:",Encoder.De_Long_Data("10 fe8e".replace(" ", "")))
        type,lens,data,dic,num = Encoder.De_Data("02 0f 11 04 12 00 48 12 00 38 0a 06 61 72 6d 76 37 31 06 00 1f 78 00 06 00 00 00 00 06 00 0d 7c 00 12 03 95 12 04 34 12 1e 61 0a 03 45 43 55 0a 14 53 4d 49 4f 53 2d 54 33 2e 30 30 31 2e 30 30 32 2e 30 30 32 0a 14 53 4d 49 4f 53 2d 54 33 2e 30 30 33 2e 30 30 30 2e 30 30 30 1c 07 e7 08 19 11 07 13 10 01 72".replace(" ", ""))
        print("dic:",dic)
        pprint.pprint(dic)

        #09 82 07 20 
       
        length = len("86 e2 9d 42 cf 91 e6 4d ed d1 56 84 14 7e 27 e3 7e e5 68 31 6e c2 5d 7c f1 a9 39 04 d1 a7 06 91 38 59 a9 34 6c cf 00 e8 c3 7d d2 22 1d a7 1b 35 65 c5 f4 1e a1 62 1a 10 49 58 6b 43 97 5d d8 66 f3 fb bc f6 cc 82 1f fd 73 7d 09 f7 01 a9 94 55 5c 76 62 97 dd 59 a0 85 95 f8 d6 3e 6b d0 22 87 ed 46 14 e3 5c 19 4a df d8 7b a3 58 b6 2f c9 60 03 21 29 fb a3 2a 23 78 79 81 ac 04 db c7 2b 1b 8d ec dc 26 f8 63 a2 b8 51 c4 27 44 14 db b8 41 75 5b f5 b8 5e f2 41 01 e3 88 6b 06 71 6f 2f d0 ec 20 99 99 67 3b d5 94 71 ed 24 67 9d 42 fd f2 e1 fd 60 d8 ed e9 b9 bb db 33 42 c9 bc 5e 09 f9 67 84 39 11 25 8f 6b 71 8a d7 72 8e a2 b3 3e 1b 7b 1e 49 ff 00 51 e5 86 b7 a4 51 3c 91 58 c2 60 44 e8 7b 2f 8c 63 c3 4a 50 52 7d e1 26 78 ad cb 2e 16 07 26 d4 69 d3 9e 88 c4 e1 bb 42 af 4d d0 0e 0d 2d 40 23 a8 6c b8 ea 59 c9 b6 e3 1a 65 8b de 18 ea 6c 83 44 c8 ba 00 7c 4d 03 8f 61 9e 70 c0 04 95 71 2b 7f 5c bf 22 d7 1f ac 64 d6 b1 e7 e6 34 fa fd 7b 22 7f cc bb 34 89 69 80 e4 8a 2e 6d fd 7c b7 a0 f2 9e 5b 44 1d 74 8f da 75 4e a7 5b c9 51 79 96 2f 03 47 48 dc cc 8e 44 9a 01 c6 1c fe eb 97 cb 0a c2 85 6e 13 84 84 e3 cc f3 00 1a 5c 03 35 d5 50 24 87 e3 84 07 dd 64 12 fd d6 1b 92 36 d7 8a 2e 8a 16 5c a1 c4 09 d5 af a2 84 32 96 2e 29 0b 56 9d fb 28 48 bf 1d 60 06 b3 0a cf a0 d0 75 f3 22 f8 2c 2a da d1 89 93 9a 0c a3 58 91 50 a7 3b 5b c8 2e d1 a7 ea 26 6b 53 29 c5 b5 49 b4 97 0c 11 48 55 8d 76 04 cd 14 d2 10 7b 23 f0 9c cf f0 01 b6 c1 d6 64 ee 6d e1 59 b0 a7 11 6a c8 da 11 ac 62 f9 96 51 0b 04 14 19 a7 44 55 fe a0 15 1e 6b 30 ef fb 2f 5f b9 ff b2 2c 4e 30 c2 48 2a e5 44 79 d6 f7 99 ed da 40 b4 bc 6f 56 79 75 ae e9 3b 66 65 e2 0e 1f 72 08 0c 4d 06 1f be 29 41 68 97 d2 de c4 5b f9 f8 db 0e aa 3f 7d 51 de 1a d0 cd 16 e7 74 ad a7 c0 92 86 c7 8a e7 60 4f b1 62 9e 3b 92 c1 e9 22 5c 08 76 a1 c9 10 75 7f 05 3e 8a ea 19 eb 74 23 f9 7e 5c d2 84 82 c8 20 4b c3 fa 8f 41 cb 9f d7 a6 30 c6 b6 ac e4 d2 f3 3d 54 c1 5f 0a 8c 65 bc 5d c6 75 f4 d3 a3 ac d7 72 81 95 fb 9c a5 97 b5 a7 71 09 ae 7a 2c 65 ef 97 83 f2 27 81 65 41 47 15 ea e5 9e 87 db cc 6e c1 d2 78 8b 2c 1a 29 0b aa a8 88 f9 50 07 1e 3b 49 e1 46 f3 37 26 ca 4f 04 c5 23 6b 10 66 23 f6 d5 7d f1 b3 5b ac 68 6a f9 8b 5f 16 6c 20 cb d2 6b 1f bd 4b 7b a7 1d 38 7e 8a 8e d5 42 23 05 0d 3f 34 59 8e 0e 62 4d f1 4a bc 50 f0 9a 2d e5 fd 67 10 ed e9 6d 30 3a 26 ad 99 55 12 9a 36 d9 cd a8 65 b5 cc 4d 58 fe 82 d1 f4 ab 93 cc 50 21 47 a8 7a cc e8 f2 0d 81 15 54 65 9c c0 57 f4 24 0f cb 35 a0 47 47 20 4f b7 91 d0 b4 02 7f 17 f1 0e c2 a0 18 ea e6 5b 01 af 59 cb 83 86 cc 0a 0b 32 fa 30 f0 85 f0 88 e5 19 d5 28 10 25 13 3c 64 6c ba b2 8c 03 0b 61 70 d3 d3 e0 1d ee 5b 5f f4 a2 25 22 a8 8a de 89 b1 2f 9e c1 7b da 09 74 c7 fe 04 81 f9 95 96 52 d7 78 8f 52 07 61 6b 05 45 40 21 20 af 36 c0 61 3e f3 c2 a0 33 ad d0 80 b8 e5 d8 a7 c4 8a 35 82 49 30 f9 10 f3 bb 2b 28 74 7d 57 bf e5 82 d4 9a d3 44 30 9c 74 b3 0a 82 93 f2 bf 2c db 57 12 63 bc 8e e5 25 6c dd 19 37 b4 d6 72 23 38 e3 10 d3 47 fe c8 d3 1f ba 52 a6 bd c6 ac ba 05 c0 db fb 75 70 69 f9 e5 f4 1b a9 31 fc 0c 4f 9f 16 b4 2d e9 7c 9f ff ec 3a cf 7c e2 a0 5f c4 a6 7d cb 6c ce 62 d2 0f 0b f5 5a 51 47 0d 21 53 dc 1a 39 f7 02 00 a9 90 41 8d fd 4d 02 65 ff 40 bb 52 d8 d5 56 de 4e dc 67 f5 42 9d ec f5 e4 df 2b 93 e4 3b 4a 22 50 e9 21 95 3d ad c9 f4 e0 7f 3c 4d d0 0d 81 de 20 3b fa ff ec b2 72 b6 e2 81 c4 c9 c7 d3 6a 6f b3 a3 d7 bb 9b 5b fa 0e 81 fd 79 a9 f7 03 e1 b4 96 3c e3 35 fd 28 44 3e c4 9c ce 93 cf 5b 19 bc a7 9e 8b a1 bb ff 27 17 d2 c9 11 44 19 90 88 9f 73 c8 68 50 8d c4 7e 11 3f a1 e8 81 44 db 81 bd 06 11 7f a6 96 28 83 a3 0c f0 a7 99 d3 fc 19 e2 35 c4 ec d0 a5 68 59 bf 24 55 45 0d f2 fa 61 71 07 87 f0 5d df 71 15 a4 ee b4 7f ff bd 9d f0 41 fb 98 50 0c fc 5f b5 3a c7 c2 bc 25 8f 56 33 8f f3 f0 70 2e 5f e1 fa f7 27 6c 30 9c 4e b7 4d 3d 95 71 fa eb 74 ea 8c 73 e7 1f 92 57 a9 12 47 12 60 87 78 88 a4 38 a2 41 9c 85 73 b7 50 bb 54 03 5c ea 96 15 1e 3b 19 21 35 39 2e a7 a1 75 e0 f3 7c a1 90 25 5c bf fa 75 96 13 d7 ff c8 f2 6d 1e 82 ff 5b 38 b1 c6 fd e3 3b b2 1f 76 97 1b 3b 0b 2a f1 08 ca 65 04 34 4a 8d c8 34 ed 3b 33 3d 9a 02 2c ad df 2c 58 4d 1d 0e 9b f4 98 f5 7e ee 5c e1 fa ed 2e 14 e6 e2 48 32 65 64 9b 28 04 ba 7d 0d 3b 9e 4e 4f a0 76 d3 03 8c 31 84 f2 c9 c1 71 4a 21 92 e4 5d 7c 32 07 bb 55 1a 19 80 88 75 a2 71 60 5d bf 4b d0 6d 7f eb 30 af 82 3d a6 3f 93 f2 30 c6 d5 a0 e4 ef 41 32 98 03 9f 76 20 d0 81 0c 79 24 65 bf 3b b6 7e 00 e6 85 56 86 a6 0e a0 d7 87 bd 64 b7 a0 43 69 32 79 3b 0a cc 9b f8 9e cf 00 4d 8d a9 93 e9 85 84 9e c2 4a 66 7a 00 36 0c e4 27 61 55 ff f6 d6 d5 a7 2c d3 62 93 ab e8 40 a1 d4 17 a0 6e 1c 89 17 d7 bb a7 c6 a4 68 73 94 0e 61 52 0e 39 05 fa c3 3b 96 34 4b 43 00 a7 ea f8 f2 c4 2a 79 04 c8 49 03 be 39 24 41 0f db e2 aa c0 fb 72 6b f0 ca 35 b0 23 d7 5a e1 3f 2e 4d 8f 37 14 97 f2 eb 2e b0 f8 cc 55 b2 f0 99 be 58 c6 22 a6 10 fb 92 75 90 4f 7e d8 93 bf 1a b8 81 27 d3 5d 29 bb 6d 89 90 5b 7f f1 88 42 28 1a 83 d2 2e c9 1e 81 70 78 ef cb 23 28 0c d4 65 c0 d2 5d ce 95 96 22 c7 db 14 50 ec 2a a1 7f ab f4 e1 e5 2f a0 ae 1f da 72 96 3c 15 58 22 ab b9 f9 ba eb a9 e4 6f 33 79 04 8e 5b cf 6c f8 bd 30 fb 78 2c a3 46 5d 9d 2d 95 a0 9d e1 a8 8d 60 c0 49 09 44 01 9d be 18 cc 79 94 f6 a9 63 76 0b ea 8b 46 65 27 c3 97 47 88 14 f6 d8 c1 d0 c3 c2 74 65 9c f9 2c 57 1a e1 05 2a 80 d6 1a a0 74 21 b5 0a 94 70 bd 02 ac fc 3f 6c 12 d0 14 e6 f7 52 8c 49 e0 42 b5 70 19 0a 36 a1 36 d2 aa 70 78 4a 74 d7 d5 e7 1d 05 af ae 2d 14 07 aa 4c ed 37 36 d5 da 7e 9c 12 1d 6f c9 5a c4 d6 67 24 78 ba 96 3e 54 d6 c8 89 d4 99 d2 63 41 d0 b2 2c 41 5f e2 dd dc c0 0d 9d 69 d5".replace(" ",""))
        print("len:", length / 2)
        print("length:", Encoder.GetVarLenStr(int(length/2)))
        print(Encoder.En_DoubleLongUnsigned(2167275520))
        oads,A_ResultRecord_Str_List,piid = ReportNotificationRecordList("880211016012030001015002020003202102002000020020010200012401031C07E8011C120F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C121E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C122D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C13000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C130F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C131E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C132D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C14000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C140F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C141E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C142D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C15000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C120F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C121E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C122D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C13000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C130F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C131E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C132D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C14000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C140F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C141E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C142D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C15000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C120F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C121E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C122D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C13000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C130F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C131E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C132D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C14000001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C140F0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C141E0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C142D0001031208841208341208A2010305000009C40500000DAC050000057801031C07E8011C15000001031208841208341208A2010305000009C40500000DAC0500000578000107E8011D000003010005".replace(" ", ""))
        print("A_ResultRecord_Str_List:", A_ResultRecord_Str_List)
        lens,oad, rcsd, dar, A_RecordRowStrList, dataStr = Encoder.De_A_ResultRecord(A_ResultRecord_Str_List[0])
        print("A_RecordRowStrList:", A_RecordRowStrList)
        print("____________________________")
        ll, csds, rcsd = Encoder.De_RCSD(rcsd)
        col = len(csds)
        for row in A_RecordRowStrList:
                lens, datas, dataStr = Encoder.De_A_RecordRow(row, col)
                print("datas:", datas)
        
        apdu = "85 01 23 24 01 07 00 01 01 05 06 00 00 27 10 06 00 00 09 6A 06 00 00 0A 00 06 00 00 0B 40 06 00 00 08 66 00 00".replace(" ", "")
        oad,result,data = GetResponseNormal(apdu)
        if (result == "00"):
            print("读取脉冲表当日电量(24010700)，总电量为1kWh、费率1为0.241kWh、费率2为0.256kWh、费率3为0.288kWh、费率4为0.215kWh, 失败 result = 0")
        else:
            if not data:
                print("读取脉冲表当日电量(24010700)，总电量为1kWh、费率1为0.241kWh、费率2为0.256kWh、费率3为0.288kWh、费率4为0.215kWh, 失败 未获取数据")
            else:
                t,lens,d,dic,num = Encoder.De_Data(data)
                if not num:
                    print("读取脉冲表当日电量(24010700)，总电量为1kWh、费率1为0.241kWh、费率2为0.256kWh、费率3为0.288kWh、费率4为0.215kWh, 失败 未获取有效个数数据")
                elif num != 5:
                    print("读取脉冲表当日电量(24010700)，总电量为1kWh、费率1为0.241kWh、费率2为0.256kWh、费率3为0.288kWh、费率4为0.215kWh, 失败 未获取正确个数数据")
                else:
                    #try:
                       pprint.pprint(dic)
                       a1 = dic[1][6] / 10000
                       a2 = dic[2][6] / 10000
                       a3 = dic[3][6] / 10000
                       a4 = dic[4][6] / 10000
                       if (a1 == 0.241) and (a2 == 0.256) and (a3 == 0.288) and (a4 == 0.215):
                           print("费率1正向有功电能示值:",a1)
                           print("费率2正向有功电能示值:",a2)
                           print("费率3正向有功电能示值:",a3)
                           print("费率4正向有功电能示值:",a4)
                           print("测试合格")
                       else:
                           print("费率1正向有功电能示值:",a1)
                           print("费率2正向有功电能示值:" ,a2)
                           print("费率3正向有功电能示值:" , a3)
                           print("费率4正向有功电能示值:" , a4)
                           print("测试失败")
                       
                    #except Exception as e:
                        #print("测试失败,e:" + str(e))
        lens,a = Encoder.De_Long64_Data("14 00 00 00 00 00 92 7c 00".replace(" ",""))
        print(lens,a)

        oad, rcsd, dar, A_RecordRowStrList, dataStr = GetResponseRecord("85 03 33 60 12 03 00 02 00 20 2a 02 00 01 30 2f 02 00 03 20 22 02 00 20 1e 02 00 20 20 02 00 01 03 55 07 05 00 00 00 00 00 01 01 03 06 00 00 00 00 1c 07 e8 01 1b 17 32 00 1c 07 e8 01 1b 17 37 00 55 07 05 00 00 00 00 00 02 01 03 06 00 00 00 00 1c 07 e8 01 1b 17 32 00 1c 07 e8 01 1b 17 37 00 55 07 05 00 00 00 00 00 03 01 03 06 00 00 00 00 1c 07 e8 01 1b 17 32 00 1c 07 e8 01 1b 17 37 00 00 00".replace(" ",""))
        print("oad:",oad)
        print("rcsd:",rcsd)
        print("dar:",dar)
        print("A_RecordRowStrList:",A_RecordRowStrList)
        print("dataStr:",dataStr)
        
        apdu = "85 03 17 60 12 03 00 02 00 20 2a 02 00 01 50 04 02 00 03 20 21 02 00 23 01 07 00 24 01 07 00 01 01 55 07 05 10 00 00 00 00 02 01 03 1c 07 e7 09 10 00 00 00 01 05 14 00 00 00 00 00 5b 8d 80 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 01 01 06 00 00 17 70 00 00".replace(" ","")
        oad, rcsd, dar, A_RecordRowStrList, dataStr = GetResponseRecord(apdu)
        col = 0
        if not rcsd:
            print("读取日冻结电量失败 0")
        else:
            lens,csds,rcsd_str = Encoder.De_RCSD(rcsd)
            print("csds:",csds)
            col = len(csds)
        if not A_RecordRowStrList:
            print("读取日冻结电量失败")
        else:
            lens, datas, dataStr = Encoder.De_A_RecordRow(A_RecordRowStrList[0],col)
            if not datas or len(datas) != 2:
                print("读取日冻结电量数据失败")
            else:
                type,lens,data,dic,num = Encoder.De_Data(datas[1])
                pprint.pprint(dic)
                zjz = dic[1][1][0][20] / 10000
                print("总加组1当日有功电量",zjz)
                mcjl = dic[2][1][0][6] / 10000
                print("脉冲计量1当日有功电量",mcjl)
                if zjz == 600 and mcjl == 0.6:
                    print("读取总加组当日电能量数据并验证成功")
                else:
                    print("读取总加组当日电能量数据并验证失败")

        data = datetime.datetime.now().strftime("%Y%m")+"16000000"
        timeS = Encoder.En_DateTimeS(data)
        frame_send = "05 03 17 60 12 03 00 05 {} 01 02 00 20 2a 02 00 01 50 04 02 00 03 20 21 02 00 23 01 07 00 24 01 07 00 00".format(timeS).replace(" ","")
        print("frame send:",frame_send)

        temp_date = datetime.datetime.now()
        date = (temp_date - relativedelta(days=temp_date.day) + relativedelta(months=2,day=1)).strftime("%Y%m%d") + "000000"
        
        timeS = Encoder.En_DateTimeS(date)
        frame_send = "05 03 1a 60 12 03 00 05 {} 01 02 00 20 2a 02 00 01 50 04 02 00 03 20 21 02 00 23 01 07 00 24 01 07 00 00".format(timeS).replace(" ","")
        print("frame send 0:",frame_send)

        apdu = "85 03 1a 60 12 03 00 02 00 20 2a 02 00 01 50 04 02 00 03 20 21 02 00 23 01 07 00 24 01 07 00 01 01 55 07 05 10 00 00 00 00 02 01 03 1c 07 e7 0a 01 00 00 00 01 05 14 00 00 00 00 00 36 ee 80 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 01 01 06 00 00 0e 10 00 00".replace(" ","")
        oad, rcsd, dar, A_RecordRowStrList, dataStr = GetResponseRecord(apdu)
        col = 0
        if not rcsd:
            print("读取总加组1当日有功电能量 0")
        else:
            lens,csds,rcsd_str = Encoder.De_RCSD(rcsd)
            col = len(csds)
            if not A_RecordRowStrList:
                print("读取总加组1当日有功电能量 1")
            else:
                lens, datas, dataStr = Encoder.De_A_RecordRow(A_RecordRowStrList[0],col)
                if not datas or len(datas) != 2:
                    print("读取总加组1当日有功电能量 2")
                else:
                    type,lens,data,dic,num = Encoder.De_Data(datas[1])
                    if not dic:
                        print("读取总加组1当日有功电能量 3")
                    else:
                        try:
                            zjz = dic[1][1][0][20] / 10000
                            print("总加组1当日有功电量:",zjz)
                            mcjl = dic[2][1][0][6] / 10000
                            print("脉冲计量1当日有功电量:",mcjl)
                            if zjz == 360 and mcjl == 0.36:
                               print("读取总加组当日电能量数据并验证成功")
                            else:
                               print("读取总加组当日电能量数据并验证失败")
                        except Exception as e:
                            print("读取日冻结电量数据失败,e:" + str(e))
        

        temp_date = datetime.datetime.now()
        date = (temp_date - relativedelta(days=temp_date.day) + relativedelta(months=2,day=1)).strftime("%Y%m%d") + "000000"
        timeS = Encoder.En_DateTimeS(date)
        frame_send = "05 03 1b 60 12 03 00 05 {} 01 02 00 20 2a 02 00 01 50 06 02 00 03 20 21 02 00 23 01 09 00 24 01 08 00 00".format(timeS).replace(" ","")
        print("frame send 1:",frame_send)


        apdu = "85 03 1b 60 12 03 00 02 00 20 2a 02 00 01 50 06 02 00 03 20 21 02 00 23 01 09 00 24 01 08 00 01 01 55 07 05 10 00 00 00 00 02 01 03 1c 07 e7 0a 01 00 00 00 01 05 14 00 00 00 00 00 92 7c 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 01 01 06 00 00 25 80 00 00".replace(" ","")

        oad, rcsd, dar, A_RecordRowStrList, dataStr = GetResponseRecord(apdu)
        col = 0
        if not rcsd:
            print("读取总加组1当月有功电能量 0")
        else:
            lens,csds,rcsd_str = Encoder.De_RCSD(rcsd)
            col = len(csds)
            if not A_RecordRowStrList:
                print("读取总加组1当月有功电能量 1")
            else:
                lens, datas, dataStr = Encoder.De_A_RecordRow(A_RecordRowStrList[0],col)
                if not datas or len(datas) != 2:
                    print("读取总加组1当月有功电能量 2")
                else:
                    type,lens,data,dic,num = Encoder.De_Data(datas[1])
                    if not dic:
                        print("读取总加组1当月有功电能量 3")
                    else:
                        try:
                            zjz = dic[1][1][0][20] / 10000
                            print("总加组1当月有功电量:",zjz)
                            mcjl = dic[2][1][0][6] / 10000
                            print("脉冲计量1当月有功电量:",mcjl)
                            if zjz == 960 and mcjl == 0.96:
                               print("读取总加组当月电能量数据并验证成功")
                            else:
                               print("读取总加组当月电能量数据并验证失败")
                        except Exception as e:
                            print("读取总加组1当月有功电能量,e:" + str(e))
        
        for i in range(1, 5):
            num = "0" + str(i)
            frame_send = "07 01 14 24 {} 03 00 02 03 51 f2 0a 02 {} 16 00 12 03 e8 00".format(num,num)
            print(frame_send, "添加 {} 路脉冲配置单元".format(i))